<!DOCTYPE html>
<html>
<head>
    <title>Electric Potential vs. Position</title>
</head>
<body>
    <canvas id="physics-canvas" width="550" height="400" style="border:1px solid #d3d3d3;"></canvas>
    <script>
        const canvas = document.getElementById('physics-canvas');
        const ctx = canvas.getContext('2d');

        // Parameters for the coordinate system
        const width = canvas.width;
        const height = canvas.height;
        const originX = 280;
        const originY = 250;
        const scaleX = 30;
        const scaleY = 15;

        // Helper functions to convert graph coordinates to canvas coordinates
        function toCanvasX(x) {
            return originX + x * scaleX;
        }

        function toCanvasY(y) {
            return originY - y * scaleY;
        }

        // Clear canvas
        ctx.clearRect(0, 0, width, height);
        ctx.fillStyle = 'white';
        ctx.fillRect(0, 0, width, height);
        ctx.fillStyle = 'black';

        // Draw Axes
        ctx.beginPath();
        ctx.strokeStyle = 'black';
        ctx.lineWidth = 1;

        // X-axis with arrowhead
        ctx.moveTo(30, originY);
        ctx.lineTo(width - 10, originY);
        ctx.lineTo(width - 20, originY - 5);
        ctx.moveTo(width - 10, originY);
        ctx.lineTo(width - 20, originY + 5);

        // Y-axis with arrowhead
        ctx.moveTo(originX, height - 20);
        ctx.lineTo(originX, 10);
        ctx.lineTo(originX - 5, 20);
        ctx.moveTo(originX, 10);
        ctx.lineTo(originX + 5, 20);
        ctx.stroke();

        // Draw axis labels
        ctx.font = 'italic 16px "Times New Roman"';
        ctx.fillText('V', originX + 10, 20);
        ctx.fillText('x', width - 25, originY + 20);
        ctx.font = '16px "Times New Roman"';
        ctx.fillText('/V', originX + 22, 20);
        ctx.fillText('/m', width - 15, originY + 20);
        ctx.fillText('O', originX - 15, originY + 18);


        // Draw ticks and number labels
        ctx.font = '14px Arial';
        // X-axis ticks and labels
        for (let i = -8; i <= 8; i++) {
            if (i !== 0) {
                ctx.beginPath();
                ctx.moveTo(toCanvasX(i), originY - 4);
                ctx.lineTo(toCanvasX(i), originY + 4);
                ctx.stroke();
            }
        }
        ctx.fillText('-5', toCanvasX(-5) - 8, originY + 20);
        ctx.fillText('5', toCanvasX(5) - 4, originY + 20);

        // Y-axis ticks and labels
        const yValues = [6, 12, -6, -12];
        for (const val of yValues) {
            ctx.beginPath();
            ctx.moveTo(originX - 4, toCanvasY(val));
            ctx.lineTo(originX + 4, toCanvasY(val));
            ctx.stroke();
            ctx.fillText(val, originX - 30, toCanvasY(val) + 5);
        }

        // Define graph points based on analysis
        const p_a = {x: -7, y: 0};
        const p_b = {x: -5, y: 12};
        const p_c = {x: -2, y: 12};
        const p_d = {x: 0, y: 6}; // For label 'd'
        const p_e = {x: 2, y: 0};
        const p_f = {x: 3, y: -6};
        const p_g = {x: 4, y: -6};
        const p_h = {x: 6, y: 0};

        // Draw the potential graph line
        ctx.beginPath();
        ctx.moveTo(toCanvasX(p_a.x), toCanvasY(p_a.y));
        ctx.lineTo(toCanvasX(p_b.x), toCanvasY(p_b.y));
        ctx.lineTo(toCanvasX(p_c.x), toCanvasY(p_c.y));
        ctx.lineTo(toCanvasX(p_e.x), toCanvasY(p_e.y)); // Connects c, d, e
        ctx.lineTo(toCanvasX(p_f.x), toCanvasY(p_f.y));
        ctx.lineTo(toCanvasX(p_g.x), toCanvasY(p_g.y));
        ctx.lineTo(toCanvasX(p_h.x), toCanvasY(p_h.y));
        ctx.lineWidth = 2;
        ctx.strokeStyle = 'black';
        ctx.stroke();

        // Draw dashed lines
        ctx.beginPath();
        ctx.setLineDash([4, 4]);
        ctx.lineWidth = 1;
        ctx.strokeStyle = 'black';
        // From b to x-axis
        ctx.moveTo(toCanvasX(p_b.x), toCanvasY(p_b.y));
        ctx.lineTo(toCanvasX(p_b.x), originY);
        // From c to x-axis
        ctx.moveTo(toCanvasX(p_c.x), toCanvasY(p_c.y));
        ctx.lineTo(toCanvasX(p_c.x), originY);
        // From f to x-axis
        ctx.moveTo(toCanvasX(p_f.x), toCanvasY(p_f.y));
        ctx.lineTo(toCanvasX(p_f.x), originY);
        // From g to x-axis
        ctx.moveTo(toCanvasX(p_g.x), toCanvasY(p_g.y));
        ctx.lineTo(toCanvasX(p_g.x), originY);
        ctx.stroke();
        ctx.setLineDash([]); // Reset to solid line

        // Draw point labels
        ctx.fillStyle = 'black';
        ctx.font = 'italic 18px "Times New Roman"';
        ctx.fillText('a', toCanvasX(p_a.x) - 20, toCanvasY(p_a.y) + 5);
        ctx.fillText('b', toCanvasX(p_b.x) - 15, toCanvasY(p_b.y) - 5);
        ctx.fillText('c', toCanvasX(p_c.x) + 5, toCanvasY(p_c.y) - 5);
        ctx.fillText('d', toCanvasX(p_d.x) + 8, toCanvasY(p_d.y) + 5);
        ctx.fillText('e', toCanvasX(p_e.x) + 5, toCanvasY(p_e.y) + 18);
        ctx.fillText('f', toCanvasX(p_f.x) - 10, toCanvasY(p_f.y) - 5);
        ctx.fillText('g', toCanvasX(p_g.x) + 5, toCanvasY(p_g.y) + 20);
        ctx.fillText('h', toCanvasX(p_h.x), toCanvasY(p_h.y) - 8);

        // Draw bottom caption
        ctx.font = '16px "SimSun", "Microsoft YaHei"'; // Common Chinese fonts
        ctx.textAlign = 'center';
        ctx.fillText('习题 7-33 图', width / 2, height - 15);
    </script>
</body>
</html>